/**
 * Created by tangqiaojie <2482366539@qq.com> on 2020/4/16 0016
 */
import { nextTick,autoLoad } from '../../lib/utils.js';
import {saveIncome2,saveIncome1,saveIncome3,saveIncome4} from './asyncTable.js';

const isAllReady=(win)=>{
  return win.$("#btsblb li:eq(0) .state").html().trim()==="已填写" &&
    win.$("#btsblb li:eq(1) .state").html().trim()==="已填写" &&
    win.$("#btsblb li:eq(2) .state").html().trim()==="已填写" &&
    win.$("#btsblb li:eq(3) .state").html().trim()==="已填写";
}

const incomeReport=()=>{
  //autoLoad();
  nextTick(()=>{
    if($("#contentiframe").contents().find("#ente_home").length){
      $("#contentiframe")[0].contentWindow.entryClickSb();
      nextTick(()=>{
        if($("#contentiframe").contents().find("#mainIframe").contents().find("#btsblb li:eq(1) .state").length){
          let win=$("#contentiframe").contents().find("#mainIframe")[0].contentWindow;
          setTimeout(()=>{
            commit(win);
          },3000);
          /*if(win.$(".topbox .btns input").length<=1){
            if(isAllReady(win)){
              commit(win);
            }else{
              saveTable2(win);
            }
          }else{
            win.initData("initData");
            nextTick(()=>{
              if(win.$('.xubox_msg.xubox_text').length && win.$('.xubox_msg.xubox_text').html()=="初始化成功!"){
                saveTable2(win);
                return true;
              }
            })
          }*/
          return true;
        }
      })
      return true;
    }
  })
};

const saveTable2=(win)=>{
  win.jumpToSBB('10412002','A201010Action.action?sign=initPage','g1011321701000006861220010310412','100')
  nextTick(()=>{
    if(win.$ && win.$(".dbt").html() && win.$(".dbt").html().includes("免税收入、减计收入、所得减免等优惠明细表") && !win.$("#loading-box").length){
      saveIncome2(win.$("#tab1").html());
      setTimeout(()=>{
        win.doTempSave();
        nextTick(()=>{
          if(win.$(".xubox_layer .xubox_yes").length){
            saveTable3(win);
            return true;
          }
        })
      },1000);
      return true;
    }
  },2000)
}

const saveTable3=(win)=>{
  win.chooseBB('10412003','A201020Action.action?sign=initPage','100','100');
  nextTick(()=>{
    if(win.$ && win.$(".dbt").html()==="A201020资产加速折旧、摊销(扣除)优惠明细表" && !win.$("#loading-box").length){
      saveIncome3(win.$("#tab1").html());
      setTimeout(()=>{
        win.doTempSave();
        nextTick(()=>{
          if(win.$(".xubox_layer .xubox_yes").length){
            saveTable1(win);
            return true;
          }
        })
      },1000);
      return true;
    }
  },2000)
}

const saveTable1=(win)=>{
  win.chooseBB('10412001','A200000Action.action?sign=initPage','100','100');
  nextTick(()=>{
    if(win.$ && win.$(".dbt").html()==="A200000 中华人民共和国企业所得税月(季)度预缴纳税申报表(A类，2018版)" && !win.$("#loading-box").length){
      win.layer.closeAll();
      setTimeout(()=>{
        win.$("[name='a200000VO.qccyrs']").focus();
        win.$("[name='a200000VO.qccyrs']").val(1);
        win.$("[name='a200000VO.qccyrs']").blur();
        win.$("[name='a200000VO.qmcyrs']").focus();
        win.$("[name='a200000VO.qmcyrs']").val(1);
        win.$("[name='a200000VO.qmcyrs']").blur();
        setTimeout(()=>{
          win.readData();
          nextTick(()=> {
            if (win.$(".xubox_yes.xubox_botton1").length) {
              win.$(".xubox_yes.xubox_botton1").click();
              setTimeout(()=>{
                saveIncome1(win.$(".tb:eq(0)").html()+win.$("#tab1").html());
                setTimeout(()=>{
                  win.doTempSave();
                  nextTick(()=>{
                    if(win.$(".xubox_yes.xubox_botton2").length) {
                      win.$(".xubox_yes.xubox_botton2").click();
                      nextTick(()=>{
                        if(win.$(".xubox_layer .xubox_yes").length){
                          saveTable4(win);
                          return true;
                        }
                      },500);
                      return true;
                    }
                  },500)
                },1000)
              },1000);
              return true;
            }
          })
        },1000)
      })
      return true;
    }
  },2000)
}

const saveTable4=(win)=>{
  win.chooseBB('10412004','A201030Action.action?sign=initPage','100','100');
  nextTick(()=>{
    if(win.$ && win.$(".dbt").html()==="A201030\t减免所得税优惠明细表" && !win.$("#loading-box").length){
      saveIncome4(win.$("#tab1").html());
      setTimeout(()=>{
        win.doTempSave();
        nextTick(()=>{
          if(win.$(".xubox_layer .xubox_yes").length){
            window.localStorage.setItem("incomeInit",1);
            $("#contentiframe")[0].contentWindow.$(".tabs-close").click();
            nextTick(()=>{
              if($("#contentiframe").contents().find("#ente_home").length){
                $("#contentiframe")[0].contentWindow.entryClickSb();
                nextTick(()=>{
                  if($("#contentiframe").contents().find("#mainIframe").contents().find("#btn10406").length){
                    let win=$("#contentiframe").contents().find("#mainIframe")[0].contentWindow;
                    setTimeout(()=>{
                      commit(win);
                    },3000);
                    return true;
                  }
                })
                return true;
              }
            })
            return true;
          }
        },500)
      },1000);
      return true;
    }
  },2000)
}

const commit=(win)=>{
  let incomeInit=window.localStorage.getItem("incomeInit");
  if(!incomeInit){
    win.initData("initData");
    nextTick(()=>{
      if(win.$('.xubox_msg.xubox_text').length && win.$('.xubox_msg.xubox_text').html()==="初始化成功!"){
        saveTable2(win);
        return true;
      }else if(win.$('.xubox_msg.xubox_text').length && win.$('.xubox_msg.xubox_text').html().includes("核心征管服务发生业务异常")){
        saveTable2(win);
        return true;
        /*window.postMessage({
          event: 'zeroReportFail',
          data: {
            message: '企业所得税申报失败，请手动申报'
          }
        }, '*');*/
      }
    },1000)
  }else if(win.$(".topbox .btns input").length<=1){
    if(win.$(".sbstate").html().trim().includes("已提交待审核")){
      setTimeout(()=>{
        window.location.reload();
      },30000)
    }else if(win.$(".sbstate").html().trim()==="申报成功"){
      window.localStorage.setItem("incomeReady",1);
      window.setTimeout(()=>{
        window.close();
      },2000)
    }
  }else if(win.$(".sbstate").html().trim().includes("申报失败")){
    window.postMessage({
      event: 'zeroReportFail',
      data: {
        message: '企业所得税申报失败，请手动申报'
      }
    }, '*');
    window.setTimeout(()=>{
      window.localStorage.setItem("incomeReady",1);
      window.close();
    },2000)
  }else{
    win.doSbConfirm();
    nextTick(()=>{
      if(win.$(".xubox_layer .xubox_yes").length){
        win.$(".xubox_layer .xubox_yes").click();
        setTimeout(()=>{
          window.location.reload();
        },30000);
        return true;
      }
    },2000)
  }

}

export {
  incomeReport
};
